package util;

import javax.net.ssl.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

public class HttpsRequest {

    public static String sendGet(String url, String param) throws Exception {
        String result = "";
        BufferedReader in = null;
           try {
               String urlStr = url + "?" + param;
               
               SSLContext sc = SSLContext.getInstance("SSL");
               sc.init(null, new TrustManager[] { new TrustAnyTrustManager() },
                        new java.security.SecureRandom());
               URL realUrl = new URL(urlStr);
               // 打开和URL之间的连接
               HttpsURLConnection connection = (HttpsURLConnection) realUrl.openConnection();
               //设置https相关属性
               connection.setSSLSocketFactory(sc.getSocketFactory());
               connection.setHostnameVerifier(new TrustAnyHostnameVerifier());
               connection.setDoOutput(true);
               
               // 设置通用的请求属性
               connection.setRequestProperty("accept", "*/*");
               connection.setRequestProperty("connection", "Keep-Alive");
               connection.setRequestProperty("user-agent",
                       "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
               // 建立实际的连接
               connection.connect();
               
               // 定义 BufferedReader输入流来读取URL的响应
               in = new BufferedReader(new InputStreamReader(connection.getInputStream(),"UTF-8"));
               String line;
               while ((line = in.readLine()) != null) {
                   result += line;
               }
           } catch (Exception e) {
               throw e;
           } finally {
               // 使用finally块来关闭输入流
               try {
                   if (in != null) {
                       in.close();
                   }
               } catch (Exception e2) {
                   throw e2;
               }
           }
           return result;
    }
    
    public static String sendPost(String url, String param) throws Exception {
        String result = "";
        BufferedReader in = null;
           try {
               String urlStr = url;
               
               SSLContext sc = SSLContext.getInstance("SSL");
               sc.init(null, new TrustManager[] { new TrustAnyTrustManager() },
                        new java.security.SecureRandom());
               URL realUrl = new URL(urlStr);
               // 打开和URL之间的连接
               HttpsURLConnection connection = (HttpsURLConnection) realUrl.openConnection();
               //设置https相关属性
               connection.setSSLSocketFactory(sc.getSocketFactory());
               connection.setHostnameVerifier(new TrustAnyHostnameVerifier());
               connection.setDoOutput(true);
               connection.setDoInput(true);
               connection.setRequestMethod("POST");
               
               // 设置通用的请求属性
               connection.setRequestProperty("accept", "*/*");
               connection.setRequestProperty("connection", "Keep-Alive");
               connection.setRequestProperty("user-agent",
                       "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
               connection.getOutputStream().write(param.getBytes("UTF-8")); 
               connection.getOutputStream().flush();  
               connection.getOutputStream().close();  
               // 建立实际的连接
//               connection.connect();
               
               // 定义 BufferedReader输入流来读取URL的响应
               in = new BufferedReader(new InputStreamReader(connection.getInputStream(),"UTF-8"));
               String line;
               while ((line = in.readLine()) != null) {
                   result += line;
//                   System.out.println("result="+result);
               }
           } catch (Exception e) {
               throw e;
           } finally {
               // 使用finally块来关闭输入流
               try {
                   if (in != null) {
                       in.close();
                   }
               } catch (Exception e2) {
                   throw e2;
               }
           }
           return result;
    }
    
    private static class TrustAnyTrustManager implements X509TrustManager {
        
        public void checkClientTrusted(X509Certificate[] chain, String authType)
                throws CertificateException {
        }
 
        public void checkServerTrusted(X509Certificate[] chain, String authType)
                throws CertificateException {
        }
 
        public X509Certificate[] getAcceptedIssuers() {
            return new X509Certificate[] {};
        }
    }
    
    private static class TrustAnyHostnameVerifier implements HostnameVerifier {
        public boolean verify(String hostname, SSLSession session) {
            return true;
        }
    }
    
}
